/**
 * 
 */
package com.otrace.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.sun.jdi.ReferenceType;
import com.sun.jdi.VirtualMachine;

/**
 * @author Prabhu KVN
 * 
 */
public class AllClassesAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		long startTime = System.currentTimeMillis();
		System.out.println("Start - execute method in:"+this.getClass().getName());
		
		HttpSession session = request.getSession(false);
		List<String> allLoadedClasses = new ArrayList<String>();
		VirtualMachine virtualMachine = (VirtualMachine) session
				.getValue("JVM");
		if (virtualMachine != null) {
			List<ReferenceType> allClasses = virtualMachine.allClasses();
			if (allClasses != null) {
				for (ReferenceType type : allClasses) {
					if (!type.name().contains("java.")
							&& !type.name().contains("javax.")
							&& !type.name().contains("sun.")
							&& !type.name().contains("com.sun.")
							&& !type.name().contains("org.")
							&& !type.name().contains("$")
							&& !type.name().contains("EDU.")
							&& !type.name().contains("bsh.")
							&& !type.name().contains("com.arjuna")
							&& !type.name().contains("gnu.")
							&& !type.name().contains("javassist.")
							&& !type.name().contains("annotation.")
							&& !type.name().contains("[]")) {

						allLoadedClasses.add(type.name());

					}

				}
			} else {
				allLoadedClasses.add("No Classes Loaded.");
			}

		} else {
			allLoadedClasses.add("No Classes Loaded.");
		}
		request.setAttribute("allClasses", allLoadedClasses);
		long endTime = System.currentTimeMillis();
		System.out.println("Finish execute method: total time - "+(endTime-startTime));
	
		return mapping.findForward("success");

	}

}
